<template>
  <div class="home-wrapper">
    <div class="header">
        <div style="font-size: 18px; color: #409BFF">多版本语言多页文稿校对检测系统</div>
        <div class="header-user">
          <div style="display: flex; align-items: center;">
            <el-dropdown @command="handleUserCommand" class="no-hover-border">
              <span class="el-dropdown-link" style="display: flex; align-items: center; cursor: pointer; outline: none; box-shadow: none;">
                <img src="@/assets/images/user.png" alt="" style="width: 28px; height: 28px; margin-right: 8px;" />
                <span>{{userName}}</span>
                <el-icon style="margin-left: 4px;"><i class="el-icon-arrow-down"></i></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="logout" @click="handleLoginout">退出</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
    </div>
    <div class="home-content">
      <div style="display: flex; justify-content: space-between; align-items: center; padding: 30px 30px 0 30px">
        <div class="font-fff"><el-button :icon="Back" @click="handleBack">返回</el-button></div>
        <div>
          <el-button type="primary" :icon="Plus" @click="handleCreate">新建</el-button>
        </div>
      </div>
      <div class="card-flex">
        <el-table :data="taskList" border style="width: 100%">
          <el-table-column type="index" width="50">
            <template #default="{ $index }">
              {{ $index + 1 }} <!-- 因为数组索引从0开始，所以这里加1 -->
            </template>
          </el-table-column>
          <el-table-column prop="name1" label="比对文件1" />
          <el-table-column prop="name2" label="比对文件2" />
          <el-table-column prop="createdTime" label="比对时间" />
          <el-table-column prop="type" label="类型" />
          <el-table-column label="比对详情" min-width="30">
            <template #default="scope">
              <span style="color: #409eff; cursor: pointer; font-size: 14px" @click="compareResult(scope.row)">
                查看详情
              </span>
            </template>
          </el-table-column>
          <el-table-column label="比对报告" min-width="30">
            <template #default="scope">
              <span v-if="!scope.row.reportState || scope.row.reportState == '暂无报告'" style="color: #409eff; cursor: pointer; font-size: 14px" @click="generateReportFn(scope.row)">
                生成报告
              </span>
              <span v-else-if="scope.row.reportState == '生成完成'" style="color: #409eff; cursor: pointer; font-size: 14px" @click="downloadResult(scope.row)">
                查看报告
              </span>
              <span v-else style="color: rgba(255,255,255,0.6); font-size: 14px">
                {{scope.row.reportState}}
              </span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <UploadModal ref="uploadModal" />
  </div>
</template>

<script setup>
import { Delete, Edit, Plus, Share, Check, Back } from '@element-plus/icons-vue'
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { getTaskList, queryTaskInfo, generateReport } from '@/api/user'
import UploadModal from '@/components/UploadModal.vue'

import { useRouter } from 'vue-router'
const router = useRouter()
const taskList = ref([])
const pageSize = 124
const currentPage = ref(1)
const pagedData = computed(() => {
const start = (currentPage.value - 1) * pageSize
  return data.slice(start, start + pageSize)
})
const uploadModal = ref(null);
const userName = ref('');

onMounted(() => {
  queryListFn();
  const info = JSON.parse(localStorage.getItem('userInfo'));
  userName.value = info.username;
  startTimer();
})

onUnmounted(() => {
  stopTimer();
})

function handlePageChange(page) {
  currentPage.value = page
}

const dialogVisible = ref(false)
const uploadBookVisible = ref(false)
const uploadPDFVisible = ref(false)
const selectedType = ref('')

const listTimer = ref(null);

const startTimer = () => {
  listTimer.value = setInterval(() => {
    queryListFn();
  }, 5000)
}

const stopTimer = () => {
  if (listTimer.value !== null) {
    clearInterval(listTimer.value);
    listTimer.value = null;
  }
}

const queryListFn = async () => {
  const { success, result, message } = await getTaskList(); 
  if (success) {
    taskList.value = result;
  }
}

const compareResult = async (item) => {
  console.log(item.id);
  const { success, result, message } = await queryTaskInfo({id: item.id}); 
  if (success) {
    let infoDetail = JSON.parse(result.content);
    infoDetail.nameInfo = {
      name1: result.name1,
      name2: result.name2
    }
    localStorage.setItem('res', JSON.stringify(infoDetail));
    if (item.type.indexOf('书刊') > -1) {
      router.push({ name:'Compare', params:{ res: '1111'} })
    } else {
      router.push({ name:'ComparePdf', params:{ res: '1111'} })
    }
    // router.push({ name:'Compare', params:{ res: '1111'} })
  }
}

const generateReportFn = async (item) => {
  const { success } = await generateReport({id: item.id}); 
  if (success) {
    queryListFn();
  }
}
const downloadResult = (item) => {
  const downloadUrl = item.report;
  window.open(downloadUrl);
}

const gotoCompare = ()=>{
  router.push({ name:'Compare', params:{ res: '1111'} })
  // router.push('/compare')
}

const handleLoginout = ()=>{
  localStorage.removeItem('token');
  localStorage.removeItem('userInfo');
  router.push({ name:'Login' });
}

const handleCreate = ()=>{
  uploadModal.value.openModal();
}

const handleBack = () => {
  router.go(-1)
}

</script>

<style scoped>
.create-icon{
  margin-bottom: 10px;
}

.font-fff{
  color:#fff
}
.header-user{
 color: #fff;
 font-size: 14px;
}
.header-img{

}
.header{
  width: 100%;
  height: 64px;
  line-height: 64px;
  background-color: #1B1E25;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 15px;
  padding-right: 15px;
}

.home-wrapper {
  position: relative;
  width: 100%;
  height: 100vh;
  background: #131218;
}

.home-content {
  height: calc(100vh - 64px);
  overflow: auto;
}
.card-list {
  /* padding:0 16px; */
}

.card-flex {
  padding: 30px;
}

.file-card {
  flex: 0 0 calc((100% - (var(--cols) - 1) * 16px) / var(--cols)); 
  height: 424px;
  /* margin: 16px; */
  box-sizing: border-box;
  background-color: #1B1E25;
  border: 1px solid #2D2F37;
}
.file-card:nth-child(7n) {
  margin-right: 0;
}

.file-card .el-card {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  /* padding: 32px 12px 24px 12px; */
  box-sizing: border-box;
}
.cover-box {
  width: 100%;
  height: 240px;
  box-sizing: border-box;
  padding: 18px 12px 18px 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  /* background-color: #1B1E25 ; */
}
.cover {
  width: 80%;
  height: 100%;
  object-fit: cover;
  border-radius: 5px;
  margin: 0;
}
.file-info {
  background-color: #16171B;
  border: 1px solid #2D2F37;
  height: 184px;
  margin-bottom: 12px;
  padding: 40px;
}
.file-name {
  font-weight: bold;
  font-size: 16px;
  color: #fff;
  margin-bottom: 8px;
  word-break: break-all;
}
.file-meta {
  color: #888;
  font-size: 14px;
}
.result-btn {
  cursor: pointer;
  margin-top: 12px;
  height: 32px;
  line-height: 32px;
  text-align: center;
  width: 100%;
  color: #409BFF;
  font-size: 14px;
  background-color: #1B1E25;
  border: 1px solid #2D2F37 ;
}
.result-btn:hover {
  border: 1px solid #409BFF ;
}
.pagination-wrapper {
  position: fixed;
  right: 10px;
  bottom: 0;
  z-index: 10;
  border-radius: 8px;
  padding: 12px 24px;
}
</style> 